package com.hxd.otc.module.order.fragment;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Space;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.LazyFragment;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.utils.StatusBarHelper;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作  者 :  zjl
 * 时  间 :  2018/6/4 17:04
 * 描  述 :  ${订单页面}
 */
public class HomeOrderFragment extends LazyFragment {

    @BindView(R.id.tv_red)
    TextView tvRed;
    @BindView(R.id.tv_red_ad)
    TextView tvRedAd;
    @BindView(R.id.rb_ad)
    TextView rbAd;
    @BindView(R.id.rb_normal)
    TextView rbNormal;
    @BindView(R.id.status_bar)
    Space mStatusBar;

    private FragmentManager fragmentManager;
    OrderFragment normalFra;
    AdOrderFragment adFra;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    protected void initView() {
        rbNormal.setSelected(true);
    }

    private void hideFragments(FragmentTransaction transaction) {

        if (normalFra != null) {
            transaction.hide(normalFra);
        }
        if (adFra != null) {
            transaction.hide(adFra);
        }

    }

    private void setTabSelection(int position) {
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (position) {
            case 0:
                if (normalFra == null) {
                    normalFra = new OrderFragment();
                    Bundle bundle = new Bundle();
                    normalFra.setArguments(bundle);
                    transaction.add(R.id.container, normalFra);
                } else {
                    transaction.show(normalFra);
                }
                break;
            case 1:
                if (adFra == null) {
                    adFra = new AdOrderFragment();
                    Bundle bundle = new Bundle();
                    adFra.setArguments(bundle);
                    transaction.add(R.id.container, adFra);
                } else {
                    transaction.show(adFra);
                }
                break;
        }
        transaction.commit();
    }


    @Override
    protected void initData() {
        fragmentManager = getChildFragmentManager();
        setTabSelection(0);
    }

    @Override
    protected void initEvent() {

    }

    @OnClick({R.id.rb_ad, R.id.rb_normal})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rb_ad:
                rbAd.setSelected(true);
                rbNormal.setSelected(false);
                setTabSelection(1);
                break;
            case R.id.rb_normal:
                rbAd.setSelected(false);
                rbNormal.setSelected(true);
                setTabSelection(0);
                break;

        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mStatusBar.setMinimumHeight(StatusBarHelper.getStatusbarHeight(getContext()));
        }
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

}
